package com.card.manager.controller;

import java.io.InputStream;
import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContext;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.card.framework.page.model.PageModel;
import com.card.framework.util.CommonUtil;
import com.card.framework.util.ReportUtil;
import com.card.manager.domain.Aaa;
import com.card.manager.domain.Aba;
import com.card.manager.domain.Abb;
import com.card.manager.domain.CardOpDay;
import com.card.manager.domain.CardOpMonth;
import com.card.manager.domain.CardOpYear;
import com.card.manager.domain.Mab;
import com.card.manager.domain.Maf;
import com.card.manager.services.AbaService;
import com.mysql.jdbc.StringUtils;


@Controller
@RequestMapping("/aba")
public class AbaController {
	final static Logger loger = Logger.getLogger(AbaController.class);
	@Autowired(required=true) 
	private AbaService abaServices;
	
	@RequestMapping("/abaCredit")
	public String aba(){
		return "abaCredit";
	}
	
	
	@RequestMapping(value="/showCardInfo",method=RequestMethod.POST)
	@ResponseBody
	public Aba showCardInfo(@RequestParam("abaid") String abaid) {
		return abaServices.geyAbaById(abaid);
	}
	
	@SuppressWarnings({ "rawtypes", "unchecked" })
	@RequestMapping(value="/cardProcUser",method=RequestMethod.GET)
	public ModelAndView cardProcUser(HttpServletRequest request,HttpSession session,
			HttpServletResponse response) {
		SecurityContext sc = (SecurityContext)request.getSession().getAttribute("SPRING_SECURITY_CONTEXT");
		Map temp = new HashMap();
		temp.put("userId", sc.getAuthentication().getName());
		abaServices.cardProcUser(temp);
		BigDecimal totalMoney = ((BigDecimal)temp.get("CreateCardMoneyOut")).add((BigDecimal)temp.get("RechargeMoneyOut")).
		add((BigDecimal)temp.get("ChangeCardMoneyOut")).subtract((BigDecimal)temp.get("DeleteCardMoneyOut")).subtract((BigDecimal)temp.get("ReverseMoneyOut"));
		temp.put("totalMoney", totalMoney);
		return new ModelAndView("cardProcUser","cardUser",temp);
	}
	
	@RequestMapping("/sendCardPopWindow")
	public String sendCardPopWindow(HttpServletRequest request,HttpSession session,
			HttpServletResponse response) {
		return "sendCardPopWindow";
	}
	
	@RequestMapping("/showChangeCardReport")
	public String showChangeCardReport(HttpServletRequest request,HttpSession session,
			HttpServletResponse response) {
		return "changeCardReport";
	}
	
	@RequestMapping("/cardOpReport")
	public String cardOpReport(HttpServletRequest request,HttpSession session,
			HttpServletResponse response) {
		return "cardOperatorReport";
	}
	
	@RequestMapping(value="/queryChangeCardList",method=RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> queryChangeCardList(Mab mab,PageModel pageModel) {
		return 	abaServices.queryChangeCardForPage(mab,pageModel);
	}
	
	@RequestMapping(value="/queryCardOpList",method=RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> queryCardOpList(HttpServletRequest request,HttpSession session,
			HttpServletResponse response,PageModel pageModel) {
		
		String operator = request.getParameter("operator");
		String status = request.getParameter("status");
		String startDate = request.getParameter("startDate");
		String endDate = request.getParameter("endDate");
		
		if("1".equals(status))
		{
			CardOpYear tempParam = new CardOpYear();
		    tempParam.setOperator(operator);
		    if(!StringUtils.isNullOrEmpty(startDate))
		    {
			    String start = startDate.split("-")[0];
			    tempParam.setStartValue(Integer.valueOf(start));
		    }
		    if(!StringUtils.isNullOrEmpty(endDate))
		    {
			    String end = endDate.split("-")[0];
			    tempParam.setEndValue(Integer.valueOf(end));
		    }
			return 	abaServices.queryCardOpYearForPage(tempParam,pageModel);
		}else if("2".equals(status)){
			CardOpMonth tempParam = new CardOpMonth();
		    tempParam.setOperator(operator);
		    if(!StringUtils.isNullOrEmpty(startDate))
		    {
			    String start = startDate.split("-")[0]+startDate.split("-")[1];
			    tempParam.setStartValue(Integer.valueOf(start));
		    }
		    if(!StringUtils.isNullOrEmpty(endDate))
		    {
			    String end = endDate.split("-")[0]+endDate.split("-")[1];
			    tempParam.setEndValue(Integer.valueOf(end));
		    }
			return 	abaServices.queryCardOpMonthForPage(tempParam,pageModel);
		}else if("3".equals(status)){
			CardOpDay tempParam = new CardOpDay();
		    tempParam.setOperator(operator);
		    if(!StringUtils.isNullOrEmpty(startDate))
		    {
			    String start = startDate.split("-")[0]+startDate.split("-")[1]+startDate.split("-")[2];
			    tempParam.setStartValue(Integer.valueOf(start));
		    }
		    if(!StringUtils.isNullOrEmpty(endDate))
		    {
			    String end = endDate.split("-")[0]+endDate.split("-")[1]+endDate.split("-")[2];
			    tempParam.setEndValue(Integer.valueOf(end));
		    }
			return abaServices.queryCardOpDayForPage(tempParam,pageModel);
		}
		
		return null;
	}
	
	@SuppressWarnings({ "rawtypes", "unchecked" })
	@RequestMapping(value="/exportCardOpForPdf",method=RequestMethod.POST)
	public void exportCardOpForPdf(HttpServletRequest request, HttpServletResponse response) {
		String operator = request.getParameter("operator");
		String status = request.getParameter("status");
		String startDate = request.getParameter("startDate");
		String endDate = request.getParameter("endDate");
		
		if("1".equals(status))
		{
			CardOpYear tempParam = new CardOpYear();
		    tempParam.setOperator(operator);
		    if(!StringUtils.isNullOrEmpty(startDate))
		    {
			    String start = startDate.split("-")[0];
			    tempParam.setStartValue(Integer.valueOf(start));
		    }
		    if(!StringUtils.isNullOrEmpty(endDate))
		    {
			    String end = endDate.split("-")[0];
			    tempParam.setEndValue(Integer.valueOf(end));
		    }
			InputStream inputStream =   this.getClass().getClassLoader().getResourceAsStream("reports/cardOpYearReport.jasper");
			Map<String, Object> tempList = abaServices.queryCardOpYearForPage(tempParam,null);
		    ReportUtil.exportPdf("卡操作统计报表", inputStream, response, new HashMap(), (List<Mab>)tempList.get("rows"));
		}else if("2".equals(status)){
			CardOpMonth tempParam = new CardOpMonth();
		    tempParam.setOperator(operator);
		    if(!StringUtils.isNullOrEmpty(startDate))
		    {
			    String start = startDate.split("-")[0]+startDate.split("-")[1];
			    tempParam.setStartValue(Integer.valueOf(start));
		    }
		    if(!StringUtils.isNullOrEmpty(endDate))
		    {
			    String end = endDate.split("-")[0]+endDate.split("-")[1];
			    tempParam.setEndValue(Integer.valueOf(end));
		    }
			InputStream inputStream =   this.getClass().getClassLoader().getResourceAsStream("reports/cardOpMonthReport.jasper");
			Map<String, Object> tempList = abaServices.queryCardOpMonthForPage(tempParam,null);
		    ReportUtil.exportPdf("卡操作统计报表", inputStream, response, new HashMap(), (List<Mab>)tempList.get("rows"));
		}else if("3".equals(status)){
			CardOpDay tempParam = new CardOpDay();
		    tempParam.setOperator(operator);
		    if(!StringUtils.isNullOrEmpty(startDate))
		    {
			    String start = startDate.split("-")[0]+startDate.split("-")[1]+startDate.split("-")[2];
			    tempParam.setStartValue(Integer.valueOf(start));
		    }
		    if(!StringUtils.isNullOrEmpty(endDate))
		    {
			    String end = endDate.split("-")[0]+endDate.split("-")[1]+endDate.split("-")[2];
			    tempParam.setEndValue(Integer.valueOf(end));
		    }
			InputStream inputStream =   this.getClass().getClassLoader().getResourceAsStream("reports/cardOpDayReport.jasper");
			Map<String, Object> tempList = abaServices.queryCardOpDayForPage(tempParam,null);
		    ReportUtil.exportPdf("卡操作统计报表", inputStream, response, new HashMap(), (List<Mab>)tempList.get("rows"));
		}
	}
	
	@SuppressWarnings({ "rawtypes", "unchecked" })
	@RequestMapping(value="/exportCardOpForExcel",method=RequestMethod.POST)
	public void exportCardOpForExcel(HttpServletRequest request, HttpServletResponse response) {
		String operator = request.getParameter("operator");
		String status = request.getParameter("status");
		String startDate = request.getParameter("startDate");
		String endDate = request.getParameter("endDate");
		
		if("1".equals(status))
		{
			CardOpYear tempParam = new CardOpYear();
		    tempParam.setOperator(operator);
		    if(!StringUtils.isNullOrEmpty(startDate))
		    {
			    String start = startDate.split("-")[0];
			    tempParam.setStartValue(Integer.valueOf(start));
		    }
		    if(!StringUtils.isNullOrEmpty(endDate))
		    {
			    String end = endDate.split("-")[0];
			    tempParam.setEndValue(Integer.valueOf(end));
		    }
			InputStream inputStream =   this.getClass().getClassLoader().getResourceAsStream("reports/cardOpYearReport.jasper");
			Map<String, Object> tempList = abaServices.queryCardOpYearForPage(tempParam,null);
		    ReportUtil.exportExcel("卡操作统计报表", inputStream, response, new HashMap(), (List<Mab>)tempList.get("rows"));
		}else if("2".equals(status)){
			CardOpMonth tempParam = new CardOpMonth();
		    tempParam.setOperator(operator);
		    if(!StringUtils.isNullOrEmpty(startDate))
		    {
			    String start = startDate.split("-")[0]+startDate.split("-")[1];
			    tempParam.setStartValue(Integer.valueOf(start));
		    }
		    if(!StringUtils.isNullOrEmpty(endDate))
		    {
			    String end = endDate.split("-")[0]+endDate.split("-")[1];
			    tempParam.setEndValue(Integer.valueOf(end));
		    }
			InputStream inputStream =   this.getClass().getClassLoader().getResourceAsStream("reports/cardOpMonthReport.jasper");
			Map<String, Object> tempList = abaServices.queryCardOpMonthForPage(tempParam,null);
		    ReportUtil.exportExcel("卡操作统计报表", inputStream, response, new HashMap(), (List<Mab>)tempList.get("rows"));
		}else if("3".equals(status)){
			CardOpDay tempParam = new CardOpDay();
		    tempParam.setOperator(operator);
		    if(!StringUtils.isNullOrEmpty(startDate))
		    {
			    String start = startDate.split("-")[0]+startDate.split("-")[1]+startDate.split("-")[2];
			    tempParam.setStartValue(Integer.valueOf(start));
		    }
		    if(!StringUtils.isNullOrEmpty(endDate))
		    {
			    String end = endDate.split("-")[0]+endDate.split("-")[1]+endDate.split("-")[2];
			    tempParam.setEndValue(Integer.valueOf(end));
		    }
			InputStream inputStream =   this.getClass().getClassLoader().getResourceAsStream("reports/cardOpDayReport.jasper");
			Map<String, Object> tempList = abaServices.queryCardOpDayForPage(tempParam,null);
		    ReportUtil.exportExcel("卡操作统计报表", inputStream, response, new HashMap(), (List<Mab>)tempList.get("rows"));
		}
		
		
	}
	
	@SuppressWarnings({ "rawtypes", "unchecked" })
	@RequestMapping(value="/exportChangeCardForPdf",method=RequestMethod.POST)
	public void exportChangeCardForPdf(HttpServletRequest request, HttpServletResponse response,Mab mab) {
		InputStream inputStream =   this.getClass().getClassLoader().getResourceAsStream("reports/changeCardReport.jasper");
		Map<String, Object> tempList = abaServices.queryChangeCardForPage(mab,null);
		ReportUtil.exportPdf("换卡报表", inputStream, response, new HashMap(), (List<Mab>)tempList.get("rows"));
	}
	
	@SuppressWarnings({ "rawtypes", "unchecked" })
	@RequestMapping(value="/exportChangeCardForExcel",method=RequestMethod.POST)
	public void exportChangeCardForExcel(HttpServletRequest request, HttpServletResponse response,Mab mab) {
		 InputStream inputStream =   this.getClass().getClassLoader().getResourceAsStream("reports/changeCardReport.jasper");
		 Map<String, Object> tempList = abaServices.queryChangeCardForPage(mab,null);
		 ReportUtil.exportExcel("换卡报表", inputStream, response, new HashMap(), (List<Mab>)tempList.get("rows"));
	}
	
	@SuppressWarnings({ "rawtypes", "unchecked" })
	@RequestMapping(value="/exportCardOpForUserPdf",method=RequestMethod.POST)
	public void exportCardOpForUserPdf(HttpServletRequest request, HttpServletResponse response,Mab mab) {
		InputStream inputStream =   this.getClass().getClassLoader().getResourceAsStream("reports/cardOpDayReportForUser.jasper");
		SecurityContext sc = (SecurityContext)request.getSession().getAttribute("SPRING_SECURITY_CONTEXT");
		Map temp = new HashMap();
		temp.put("userId", sc.getAuthentication().getName());
		abaServices.cardProcUser(temp);
		 List tempList = new ArrayList();
		 CardOpDay cardOpDay = new CardOpDay();
		 cardOpDay.setOperator(sc.getAuthentication().getName());
		 cardOpDay.setCreatecardnum((Integer)temp.get("CreateCardNumOut"));
		 cardOpDay.setCreatecardmoney((BigDecimal)temp.get("CreateCardMoneyOut"));
		 cardOpDay.setLossedcardnum((Integer)temp.get("LossedCardNumOut"));
		 cardOpDay.setUnlossedcardnum((Integer)temp.get("UnlossedCardNumOut"));
		 cardOpDay.setDeletecardnum((Integer)temp.get("DeleteCardNumOut"));
		 cardOpDay.setDeletecardmoney((BigDecimal)temp.get("DeleteCardMoneyOut"));
		 cardOpDay.setChangecardnum((Integer)temp.get("ChangeCardNumOut"));
		 cardOpDay.setChangecardmoney((BigDecimal)temp.get("ChangeCardMoneyOut"));
		 cardOpDay.setReversenum((Integer)temp.get("ReverseNumOut"));
		 cardOpDay.setReversemoney((BigDecimal)temp.get("ReverseMoneyOut"));
		 cardOpDay.setRechargenum((Integer)temp.get("RechargeNumOut"));
		 cardOpDay.setRechargemoney((BigDecimal)temp.get("RechargeMoneyOut"));
		 cardOpDay.setTotalMoney(cardOpDay.getCreatecardmoney().add(cardOpDay.getRechargemoney()).add(cardOpDay.getChangecardmoney()).subtract(cardOpDay.getDeletecardmoney()).subtract(cardOpDay.getReversemoney()));
		 tempList.add(cardOpDay);
		 ReportUtil.exportPdf("报表统计", inputStream, response, new HashMap(), tempList);
	}
	
	
	@SuppressWarnings({ "rawtypes", "unchecked" })
	@RequestMapping(value="/exportCardOpForUserExcel",method=RequestMethod.POST)
	public void exportCardOpForUserExcel(HttpServletRequest request, HttpServletResponse response) {
		 InputStream inputStream =   this.getClass().getClassLoader().getResourceAsStream("reports/cardOpDayReportForUser.jasper");
		 SecurityContext sc = (SecurityContext)request.getSession().getAttribute("SPRING_SECURITY_CONTEXT");
		 Map temp = new HashMap();
		 temp.put("userId", sc.getAuthentication().getName());
		 abaServices.cardProcUser(temp);
		 List tempList = new ArrayList();
		 CardOpDay cardOpDay = new CardOpDay();
		 cardOpDay.setOperator(sc.getAuthentication().getName());
		 cardOpDay.setCreatecardnum((Integer)temp.get("CreateCardNumOut"));
		 cardOpDay.setCreatecardmoney((BigDecimal)temp.get("CreateCardMoneyOut"));
		 cardOpDay.setLossedcardnum((Integer)temp.get("LossedCardNumOut"));
		 cardOpDay.setUnlossedcardnum((Integer)temp.get("UnlossedCardNumOut"));
		 cardOpDay.setDeletecardnum((Integer)temp.get("DeleteCardNumOut"));
		 cardOpDay.setDeletecardmoney((BigDecimal)temp.get("DeleteCardMoneyOut"));
		 cardOpDay.setChangecardnum((Integer)temp.get("ChangeCardNumOut"));
		 cardOpDay.setChangecardmoney((BigDecimal)temp.get("ChangeCardMoneyOut"));
		 cardOpDay.setReversenum((Integer)temp.get("ReverseNumOut"));
		 cardOpDay.setReversemoney((BigDecimal)temp.get("ReverseMoneyOut"));
		 cardOpDay.setRechargenum((Integer)temp.get("RechargeNumOut"));
		 cardOpDay.setRechargemoney((BigDecimal)temp.get("RechargeMoneyOut"));
		 cardOpDay.setTotalMoney(cardOpDay.getCreatecardmoney().add(cardOpDay.getRechargemoney()).add(cardOpDay.getChangecardmoney()).subtract(cardOpDay.getDeletecardmoney()).subtract(cardOpDay.getReversemoney()));
		 tempList.add(cardOpDay);
		 ReportUtil.exportExcel("报表统计", inputStream, response, new HashMap(), tempList);
	}
	

	@RequestMapping(value="/cardChangePopWindow",method=RequestMethod.POST)
	@ResponseBody
	public Map<String, String> changeCardPopWindow(@RequestParam("lostedDate") String lostedDate) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		Date date = null;
		try {
			date = sdf.parse(lostedDate);
		} catch (ParseException e1) {
			e1.printStackTrace();
		}
		Date nowDate = new Date();
	    long diff = nowDate.getTime() - date.getTime();
	    long days = diff / (1000 * 60 * 60 * 24);
	    Map<String, String> map = new HashMap<String, String>();
	    if(days>=7)
	    {
	    	map.put("mes", "1");
	    }else
	    {
	    	map.put("mes", "0");
	    }
		return map;
	}
	
	@RequestMapping(value="/cardDelCardChangePopWindow",method=RequestMethod.POST)
	@ResponseBody
	public Map<String, String> cardDelCardChangePopWindow(@RequestParam("lostedDate") String lostedDate) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		Date date = null;
		try {
			date = sdf.parse(lostedDate);
		} catch (ParseException e1) {
			e1.printStackTrace();
		}
		Date nowDate = new Date();
	    long diff = nowDate.getTime() - date.getTime();
	    long days = diff / (1000 * 60 * 60 * 24);
	    Map<String, String> map = new HashMap<String, String>();
	    if(days>=7)
	    {
	    	map.put("mes", "1");
	    }else
	    {
	    	map.put("mes", "0");
	    }
		return map;
	}
	
	@RequestMapping("/cardChangeO")
	public String openCardPopWindow() {
		return "changeCardPopWindow";
	}
	
	@RequestMapping(value="/queryList",method=RequestMethod.POST)
	@ResponseBody
	public List<Aba> queryList(@RequestParam("userid") Long userid){
		return abaServices.selectAbaForPage(userid);
	}
	
	@RequestMapping(value="/cardChange")
	public String abaChange(){
		return "abaChange";
	}
	
	@RequestMapping(value="/cardLoss")
	public ModelAndView cardLoss(){
		ModelAndView view = new ModelAndView();
		view.addObject("type", "2");
		view.addObject("operName", "挂失");
		view.setViewName("abaCardOper");
		return view;
	}
	
	@RequestMapping(value="/cardUnLoss")
	public ModelAndView cardUnLoss(){
		ModelAndView view = new ModelAndView();
		view.addObject("type", "1");
		view.addObject("operName", "解挂");
		view.setViewName("abaCardOper");
		return view;
	}
	
	@RequestMapping(value="/cardDel")
	public ModelAndView cardDel(){
		ModelAndView view = new ModelAndView();
		view.addObject("type", "3");
		view.addObject("operName", "账户解除");
		view.setViewName("abaCardOper");
		return view;
	}
	
	@RequestMapping(value="/cardChangeC",method=RequestMethod.POST)
	@ResponseBody
	public Map<String, String> cardChangeC(HttpServletRequest request,HttpSession session,
			HttpServletResponse response){
		Map<String, String> map = new HashMap<String, String>();
		String eflag = request.getParameter("eflag");
		String cardpassword = request.getParameter("cardpassword");
		String oldPasswordInput = request.getParameter("oldPasswordInput");
		
		if("true".equals(eflag))
		{
			if(!cardpassword.equalsIgnoreCase(oldPasswordInput))
			{
				map.put("mes", "1");
				return map;
			}
		}
		try {
			//System.out.println("000"+request.getParameter("abaidNew"));
			Aba newAba = new Aba();
			Aba oldAba = new Aba();
			String oldAbaId = request.getParameter("abaid");
			oldAba.setAbaid(oldAbaId);
			oldAba.setVersion(Long.valueOf(request.getParameter("version")));
			oldAba.setDelflg((byte)0);
			
			String newAbaId = request.getParameter("abaidNew");
			newAba.setAbaid(newAbaId);
			Long newUserId = Long.valueOf(request.getParameter("userid"));
			newAba.setUserid(newUserId);
			String cardkey = request.getParameter("cardkeyNew");
			newAba.setCardkey(cardkey);
			String cardPassword = request.getParameter("newPassword");
			newAba.setCardpassword(cardPassword);
			newAba.setLosted(1);
			System.out.println("request.getParameter(eflag)="+request.getParameter("eflag"));
			newAba.setEflag("true".equalsIgnoreCase(request.getParameter("eflag")));
			newAba.setMoneycardlimit(Integer.valueOf(request.getParameter("moneycardlimit")));
			newAba.setEmoney(Double.valueOf(request.getParameter("emoney")));
			newAba.setMoney(Double.valueOf(request.getParameter("money")));
			newAba.setDelflg((byte)1);
			newAba.setVersion(Long.valueOf(0));
			SecurityContext sc = (SecurityContext)request.getSession().getAttribute("SPRING_SECURITY_CONTEXT");
			newAba.setCreatuserid(sc.getAuthentication().getName());
			
			SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmssS");
			String itemId = sdf.format(new Date());
			Mab mabInsert = new Mab();
			mabInsert.setItemid(itemId);
			mabInsert.setOldcardid(oldAbaId);
			mabInsert.setNewcardid(newAbaId);
			int mabFlagTemp = Integer.valueOf(request.getParameter("mabFlag"));
			mabInsert.setMabflag((byte)mabFlagTemp);
			mabInsert.setSelltax(Integer.valueOf(request.getParameter("sellTax")));
			mabInsert.setCreatuserid(sc.getAuthentication().getName());
			mabInsert.setCreatdatetime(new Date());
			
			abaServices.changeCard(oldAba,newAba,mabInsert);
			
			map.put("mes", "操作成功");
		}
		catch (Throwable e) {
			CommonUtil.throwsException(map, e);
			loger.error("changeCard", e);
		}
		return map; 
	}
	
	
	@RequestMapping(value="/cardDelcardOper",method=RequestMethod.POST)
	@ResponseBody
	public Map<String, String> cardDelcardOper(HttpServletRequest request){
		Map<String, String> map = new HashMap<String, String>();
		try {
			Aba aba = new Aba();
			Maf maf = new Maf();
			String abaid = request.getParameter("abaid");
			String crId = request.getParameter("creatuserid");
			aba.setAbaid(request.getParameter("abaid"));
			aba.setVersion(Long.valueOf(request.getParameter("version")));
			aba.setLosteddate(new Date());
			aba.setCreatuserid(crId);
			maf.setCardid(abaid);
			maf.setCreatuserid(crId);
			System.out.println("ltype="+request.getParameter("ltype"));
			if("true".equals(request.getParameter("eflag"))&&!request.getParameter("cardpassword").equalsIgnoreCase(request.getParameter("newPass"))){
				map.put("mes", "电子账户密码错误,操作失败.");
				return map;
			}
			int ltype = Integer.valueOf(request.getParameter("ltype"));
			if(ltype==3){
				double delMoney = Double.valueOf(request.getParameter("delmoney"));
				maf.setDelmoney(delMoney);
			}
			maf.setMafflag((byte)ltype);
			aba.setLosted(ltype);
			abaServices.CardOper(aba, maf);
			map.put("mes", "操作成功");
		}
		catch (Throwable e) {
			CommonUtil.throwsException(map, e);
			loger.error("changeCard", e);
		}
		return map; 
	}
	@RequestMapping(value="/cardLosscardOper",method=RequestMethod.POST)
	@ResponseBody
	public Map<String, String> cardLosscardOper(HttpServletRequest request){
		Map<String, String> map = new HashMap<String, String>();
		try {
			Aba aba = new Aba();
			Maf maf = new Maf();
			String abaid = request.getParameter("abaid");
			String crId = request.getParameter("creatuserid");
			aba.setAbaid(request.getParameter("abaid"));
			aba.setVersion(Long.valueOf(request.getParameter("version")));
			aba.setLosteddate(new Date());
			aba.setCreatuserid(crId);
			maf.setCardid(abaid);
			maf.setCreatuserid(crId);
			System.out.println("ltype="+request.getParameter("ltype"));
			if("true".equals(request.getParameter("eflag"))&&!request.getParameter("cardpassword").equalsIgnoreCase(request.getParameter("newPass"))){
				map.put("mes", "电子账户密码错误,操作失败.");
				return map;
			}
			int ltype = Integer.valueOf(request.getParameter("ltype"));
			if(ltype==3){
				double delMoney = Double.valueOf(request.getParameter("delmoney"));
				maf.setDelmoney(delMoney);
			}
			maf.setMafflag((byte)ltype);
			aba.setLosted(ltype);
			abaServices.CardOper(aba, maf);
			map.put("mes", "操作成功");
		}
		catch (Throwable e) {
			CommonUtil.throwsException(map, e);
			loger.error("changeCard", e);
		}
		return map; 
	}
	@RequestMapping(value="/cardUnLosscardOper",method=RequestMethod.POST)
	@ResponseBody
	public Map<String, String> cardUnLosscardOper(HttpServletRequest request){
		Map<String, String> map = new HashMap<String, String>();
		try {
			Aba aba = new Aba();
			Maf maf = new Maf();
			String abaid = request.getParameter("abaid");
			String crId = request.getParameter("creatuserid");
			aba.setAbaid(request.getParameter("abaid"));
			aba.setVersion(Long.valueOf(request.getParameter("version")));
			aba.setLosteddate(new Date());
			aba.setCreatuserid(crId);
			maf.setCardid(abaid);
			maf.setCreatuserid(crId);
			System.out.println("ltype="+request.getParameter("ltype"));
			if("true".equals(request.getParameter("eflag"))&&!request.getParameter("cardpassword").equalsIgnoreCase(request.getParameter("newPass"))){
				map.put("mes", "电子账户密码错误,操作失败.");
				return map;
			}
			int ltype = Integer.valueOf(request.getParameter("ltype"));
			if(ltype==3){
				double delMoney = Double.valueOf(request.getParameter("delmoney"));
				maf.setDelmoney(delMoney);
			}
			maf.setMafflag((byte)ltype);
			aba.setLosted(ltype);
			abaServices.CardOper(aba, maf);
			map.put("mes", "操作成功");
		}
		catch (Throwable e) {
			CommonUtil.throwsException(map, e);
			loger.error("changeCard", e);
		}
		return map; 
	}
	
	@RequestMapping(value="/cardLoss",method=RequestMethod.POST)
	@ResponseBody
	public Aaa cardLoss(HttpServletRequest request,HttpSession session,Aaa aaa,Aba aba){
		Map<String,Object> result = abaServices.queryCardInfo(aaa,aba);	
		return (Aaa)result.get("userInfo");
	}
	@RequestMapping(value="/cardUnLoss",method=RequestMethod.POST)
	@ResponseBody
	public Aaa cardUnLoss(HttpServletRequest request,HttpSession session,Aaa aaa,Aba aba){
		Map<String,Object> result = abaServices.queryCardInfo(aaa,aba);	
		return (Aaa)result.get("userInfo");
	}
	@RequestMapping(value="/cardDel",method=RequestMethod.POST)
	@ResponseBody
	public Aaa cardDel(HttpServletRequest request,HttpSession session,Aaa aaa,Aba aba){
		Map<String,Object> result = abaServices.queryCardInfo(aaa,aba);	
		return (Aaa)result.get("userInfo");
	}
	@RequestMapping(value="/cardChangeQ",method=RequestMethod.POST)
	@ResponseBody
	public Aaa cardChangeQ(HttpServletRequest request,HttpSession session,Aaa aaa,Aba aba){
		Map<String,Object> result = abaServices.queryCardInfo(aaa,aba);	
		return (Aaa)result.get("userInfo");
	}
	
	@RequestMapping(value="/delete",method=RequestMethod.POST)
	@ResponseBody
	public Map<String, String> delete(@RequestParam("json") String json){
		Map<String, String> map = new HashMap<String, String>();
		try {
			abaServices.deleteAbas(CommonUtil.getBean(json, Aba.class));
			map.put("mes", "删除成功");
		} catch (Throwable e) {
			CommonUtil.throwsException(map, e);
			loger.error("delete", e);
		}
		return map;//重定向
	}
	
	@RequestMapping(value="/addOrUpdate",method=RequestMethod.POST)
	@ResponseBody
	public Map<String, String> addOrUpdate(Aba aba,Abb abb){
		Map<String, String> map = new HashMap<String, String>();
		try {
			if(abb!=null&&abb.getCreditflag()!=null){
				if(abb.getCreditflag()==1){
					aba.setEmoney(aba.getEmoney()+abb.getEmoneybalance());
					aba.setMoney(aba.getMoney()+abb.getMoneybalance());
					map.put("cflag", "2");
					map.put("ver", String.valueOf(aba.getVersion()+1));
				}else if(abb.getCreditflag()==2){
					aba.setEmoney(abb.getEmoneybalance());
					aba.setMoney(abb.getMoneybalance());
					abb.setEmoneybalance(abb.getEmoneybalance()+abb.getEmoney());
					abb.setMoneybalance(abb.getMoneybalance()+abb.getEmoney());
					map.put("cflag", "1");
				}
			}
			abaServices.addOrUpdate(aba,abb);
			map.put("mes", "操作成功");
		}
		catch (Throwable e) {
			CommonUtil.throwsException(map, e);
			loger.error("addOrUpdate", e);
		}
		return map; 
	}
}